package com.dingwei.lesyndrome.ui.activity;

import android.os.Bundle;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.dingwei.lesyndrome.R;
import com.dingwei.lesyndrome.bean.SiteInfoBean;
import com.dingwei.lesyndrome.mvp.presenter.AboutUsPresenter;
import com.dingwei.lesyndrome.mvp.view.Contract;
import com.lihao.baselibrary.base.BaseActivity;
import com.lihao.baselibrary.weight.LoadingProgress;
import com.lihao.baselibrary.weight.TitleBar;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created time 2018/3/9.
 * Creator 南柯一梦丶 on
 * Personal signature 余生还长 何必慌张
 * Page description :
 */

public class AboutUsActivity extends BaseActivity implements Contract.AboutUsView {

    AboutUsPresenter aboutUsPresenter = new AboutUsPresenter(this);
    @Bind(R.id.title_bar)
    TitleBar titleBar;
    @Bind(R.id.webView)
    WebView webView;
    @Bind(R.id.loading_progress)
    LoadingProgress loadingProgress;
    @Bind(R.id.data_layout)
    RelativeLayout dataLayout;
    @Bind(R.id.no_net_btn)
    TextView noNetBtn;
    @Bind(R.id.no_net_layout)
    RelativeLayout noNetLayout;

    @Override
    protected int getContentViewId() {
        return R.layout.activity_about_us;
    }

    @Override
    protected void initData() {
        aboutUsPresenter.initData();
    }

    @Override
    protected void initView() {


        WebSettings webSettings = webView.getSettings();
        // 设置WebView属性，能够执行Javascript脚本
        webSettings.setJavaScriptEnabled(true);
        // 设置可以访问文件
        webSettings.setAllowFileAccess(true);

        webView.setWebViewClient(new webViewClient());
        webView.setWebChromeClient(new WebChromeClient() {

            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                super.onProgressChanged(view, newProgress);
                if (newProgress == 100) {
                    loadingProgress.setVisibility(View.GONE);
                }
            }
        });


    }

    // Web视图
    private class webViewClient extends WebViewClient {
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }
    }

    @Override
    public void onFail() {
        showData("fail");
    }

    public void showData(String state) {
        if (state.equals("success")) {
            dataLayout.setVisibility(View.VISIBLE);
            noNetLayout.setVisibility(View.GONE);
        } else {
            dataLayout.setVisibility(View.GONE);
            noNetLayout.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void openWeb(SiteInfoBean bean) {
        showData("success");
        webView.loadUrl(bean.data.about_us_web);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }
}
